#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import re
import glob
import time
import shutil
from datetime import datetime

class PathProject(object):
    file_count = 0
    dir_count = 0
    path_count = 0
    if os.name == 'posix':
        log = open('/tmp/log.txt','a')
    else:
        log = open('c://log.txt', 'a')
    def __init__(self):
        pass
#        print 'start search...\n'
#        self.log.write('start search...\n')

    def search(self, root_path, s_args=''):
        print 'start search...\n'
        self.log.write('\nstart search...\n\n')
        for path, d_list, f_list in os.walk(root_path):
            if '.svn' not in path:
#                self.path_count = self.path_count + 1
                if s_args == '':
                    self.output_path(path)
                else:
                    self.count_path()
                self.about_dir_list(path, d_list, s_args)
                for f in f_list:
                    try:
                        if re.compile(r'^%s' % s_args).match(f):
                            file = os.path.join(path, f)
                            self.output_file(file)
                    except:
                        pass
    def delete(self, root_path, s_args=''):
        print 'start delete...\n'
        self.log.write('\nstart search...\n\n')
        for path, d_list, f_list in os.walk(root_path):
            if '.svn' not in path:
                if s_args != '':
                    self.handle_delete(path, d_list, s_args)

    def handle_delete(self, path, d_list, filter):
        filter_file_list = []
        root = os.path.join(path, filter)
        c_path = glob.glob(root)
        filter_file_list.extend(c_path)
        for dir in d_list:
            current_path = os.path.join(os.path.join(path, dir), filter)
            if glob.glob(current_path):
                filter_file_list.extend(glob.glob(current_path))
                for file in glob.glob(current_path):
                    print 'os.remove file:', file
        for file in filter_file_list:
#           os.remove(file)
#            print 'os.remove file:', file
            pass

    def about_dir_list(self, path, dir_list, filter):
        if filter == '': return
        for dir in dir_list:
            current_path = os.path.join(os.path.join(path, dir), filter)
            filter_file_list = glob.glob(current_path)
            if filter_file_list:
                self.output_dir(os.path.join(path, dir))
            for filter_file in filter_file_list:
                self.output_file(filter_file)

    def count_path(self):
        self.path_count = self.path_count + 1

    def count_dir(self):
        self.dir_count = self.dir_count + 1

    def count_file(self):
        self.file_count = self.file_count + 1

    def output_path(self, path):
        self.path_count = self.path_count + 1
        print '[%s] %s' % (datetime.now(), path)
        self.log.write('[%s] %s\n' % (datetime.now(), path))
    
    def output_dir(self, dir):
        self.dir_count = self.dir_count + 1
        print '[%s] %s' % (datetime.now(), dir)
        self.log.write('[%s] %s\n' % (datetime.now(), dir))

    def output_file(self, file):
        self.file_count = self.file_count + 1
        print '[%s] %s' % (datetime.now(), file)
        self.log.write('[%s] %s\n' % (datetime.now(), file))

    def output_help(self):
        print '[%s] %s' % (datetime.now(), '''
#######################
# Help Text:
# -h: 显示帮助信息
# -f: 显示目录文件列表
# -s: 显示搜索文件列表
#######################''')
    def close_log(self):
        print '\nstop search...\n\n'
        self.log.write('\nstop search\n\n')
        print 'search result: path:%d, dir:%d, file:%d\n' % \
                (self.path_count, self.dir_count, self.file_count)
        self.log.write('search result: path:%d, dir:%d, file:%d\n' % \
                (self.path_count, self.dir_count, self.file_count))
        self.log.close()

if __name__ == '__main__':
    pathproject = PathProject()
    args = sys.argv[1]
    arg_list = args.split('-')
    arg_list2 = []
    for arg in arg_list:
        arg_list2.append(arg.strip())
    for arg in arg_list2[1:]:
        if arg == 'h':
            pathproject.output_help()
        elif arg == 'f':
            pathproject.search(os.getcwd())
        elif arg == 's':
            s_args = raw_input('''符合开头字符串的文件名称:''')
            pathproject.search(os.getcwd(), s_args)
        elif arg == 'd':
            s_args = raw_input('''删除[X]符合开头字符串的文件名称:''')
            pathproject.delete(os.getcwd(), s_args)
        else:
            print "We don't have this argument: %s" % arg
    pathproject.close_log()
